package com.seari.c3gallery.controller;

import com.alibaba.fastjson.JSON;
import com.seari.c3gallery.main.StartUpClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class StompSocketController {

    @Autowired
    public SimpMessagingTemplate template;

    /**
     * 模式二平板控制上下页切换使用
     * @param cmd
     */
    @RequestMapping(value = "/excon")
    @ResponseBody
    public void sendViewCmd(String cmd){
//        template.convertAndSend("/keyCmd",cmd);
        template.convertAndSend("/profession",cmd);
    }

    /**
     * 模式二左右屏切换页面使用
     * @param mod
     */
    @MessageMapping("/recon")
    public void reportPageControl(String mod){
//        template.convertAndSend("/displayMod",mod);
        template.convertAndSend("/reportMod",mod);
    }

    /**
     * 模式二左右屏切换页面使用
     * @param cmd
     */
    @RequestMapping(value = "/exconp")
    @ResponseBody
    public void reportPPTControl(String cmd){
//        template.convertAndSend("/displayMod",mod);
        template.convertAndSend("/keyCmd",cmd);
    }

    /**
     * 模式三四使用
     * @param mod
     */
    @RequestMapping(value = "/daily")
    @ResponseBody
    public void switchDailyMod(String mod,String type,String position){
        if(StringUtils.isBlank(mod) && StringUtils.isBlank(position) && "refresh".equals(type)){
            Map<String,Object> map = new HashMap<>();
            map.put("refreshFlag",true);
            String jsonStr = JSON.toJSONString(map);
            template.convertAndSendToUser("report","/menuContro",jsonStr);
        }else if(StringUtils.isBlank(type) || StringUtils.isBlank(position)){
            template.convertAndSend("/dailyMod",mod);
        }else {
            Map<String,String> map = new HashMap<>();
            map.put("mod",mod);
            map.put("type",type);
            map.put("position",position);
            String mapStr = JSON.toJSONString(map);
            template.convertAndSend("/dailyMod",mapStr);
        }
    }


    /**
     * 模式一使用
     * @param type
     * @param addr
     */
    @RequestMapping(value = "/prof")
    @ResponseBody
    public void professionMod(String mod,String type,String addr){
        if(!StringUtils.isBlank(mod)){
            StartUpClass.client.callScene(mod);
            template.convertAndSend("/reportMod",mod);
        }
        Map<String,Object> payload = new HashMap<>();
        payload.put("type",type);
        payload.put("addr",addr);
        template.convertAndSend("/profession",payload);
    }

    /**
     * 模式二使用
     * @param prof
     */
    @RequestMapping(value = "/report")
    @ResponseBody
    public void reportMod(String prof,String mod){
        StartUpClass.client.callScene(mod);
//        //如果切换到模式二时 模式一中在播放视频 则清除视频
        template.convertAndSend("/profession","pause");
        template.convertAndSend("/reportMod",prof);
    }


    /**
     * 控制大屏与pad菜单同步
//     * @param mId
//     * @param state
//     * @param src
     */
    @MessageMapping(value = "/menu")
//    public void menuListContro(String mId,String state,String src){
    public void menuListContro(String data){
        Map<String,Object> transData = (Map) JSON.parse(data);
        if(transData.get("src").equals("mod")){
            template.convertAndSendToUser("report","/menuContro",data);
        } else {
            template.convertAndSendToUser("mod","/menuContro",data);
        }
    }


}
